/**
 * Adds a drop filter to label to give embossed look.
 * @author Mike Orth
 * 
 */
package com.mikeorth.controls
{
	import flash.filters.DropShadowFilter;
	
	import mx.controls.Label;
	
	[Style (name="embossColor", type="uint", format="Color")]
	
	
	public class EmbossedLabel extends Label
	{
		// TODO: should make these styles,
		// these properties will not currently reflect run-time changes
		public var embossAlpha:Number = 1;
		public var angle:Number = 45;
		public var thickness:Number = 1; 
		
		/**
		 * Color of the outlined drop filter for embossed look.
		 * 
		 * @default 0xFFFFFF 
		 */		
		private var _embossColor:uint = 0xFFFFFF;
		
		/**
		 * Flag to note if embossColor has changed. 
		 */		
		private var isEmbossColorChanged:Boolean = true;
		
		public function EmbossedLabel()
		{
			super();
			
			// initialize style if there is no default defined
			if (getStyle("embossColor") == undefined) {
				setStyle("embossColor", _embossColor);
			}
		}
		
		/**
		 * Create the drop shadow filter and add it to the control's filters array. 
		 */		
		private function createFilter():void {
			_embossColor = getStyle("embossColor");
			filters = [];
			var shadow:DropShadowFilter = new DropShadowFilter(thickness, angle, _embossColor, embossAlpha, 0,0,1);
			filters = [shadow];
		}
		
		override public function styleChanged(styleProp:String):void {
			super.styleChanged(styleProp);
			
			if (styleProp == "embossColor") {
				// trigger the filter to be recreated
				isEmbossColorChanged = true;
				invalidateDisplayList();
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (isEmbossColorChanged) {
				// if the stlye for embossColor has changed, recreate the filter
				createFilter();
				isEmbossColorChanged = false;
			}
		}
	}
}